import Position
import UserAccount

class Event {

  String name
  String description
  String location
  UserAccount administrator
  Date startDate
  Date endDate
//  SortedSet positions = new TreeSet()

  static hasMany = [positions: Position]

  static mapping = {
    cache true
  }

  static constraints = {
    name(maxSize: 40, nullable: false, blank: false)
    description(maxSize: 250, nullable: true)
    location(maxSize: 50)
//    startDate()
    startDate(nullable: false,
            validator: {
              val, obj ->
              if (val != null) {
                return (val.before(obj.endDate) || val.equals(obj.endDate))
              }
              return true
            })

//    endDate()
    endDate(nullable: false,
            validator: {
              val, obj ->
              if (val != null) {
                return (val.after(obj.startDate) || val.equals(obj.startDate))
              }
              return true
            })

    administrator()
    positions()
  }


  boolean equals(o) {
    if (this.is(o)) return true;

    if (!o || getClass() != o.class) return false;

    Event event = (Event) o;

    if (administrator ? !administrator.equals(event.administrator) : event.administrator != null) return false;
    if (description ? !description.equals(event.description) : event.description != null) return false;
    if (endDate ? !endDate.equals(event.endDate) : event.endDate != null) return false;
    if (location ? !location.equals(event.location) : event.location != null) return false;
    if (name ? !name.equals(event.name) : event.name != null) return false;
    if (startDate ? !startDate.equals(event.startDate) : event.startDate != null) return false;

    return true;
  }

  int hashCode() {
    int result;

    result = (name ? name.hashCode() : 0);
    result = 31 * result + (description ? description.hashCode() : 0);
    result = 31 * result + (location ? location.hashCode() : 0);
    result = 31 * result + (administrator ? administrator.hashCode() : 0);
    result = 31 * result + (startDate ? startDate.hashCode() : 0);
    result = 31 * result + (endDate ? endDate.hashCode() : 0);
    return result;
  }

}
